package com.hmall.cart.listener;

import com.hmall.api.domain.dto.OrderMsgDTO;
import com.hmall.cart.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class OCreateListener {

    @Autowired
    private ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue",durable = "true"),
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC),
            key = "order.create"
    ))
    public void listenerOrderCreate(OrderMsgDTO msgDTO){
        log.info("接收到订单创建的消息，用户ID:{},商品ID:{}",msgDTO.getUserId(),msgDTO.getItemIds());
        try {
            cartService.removeByItemIds(msgDTO.getItemIds());
            log.info("购物车清理成功");
        } catch (Exception e) {
            log.error("购物车清理失败",e);
        }

    }

}
